#include "twitlistitem.h"
#include <QtGui>

TwitListItem::TwitListItem(QWidget *parent):
        QFrame(parent)
{
    //Objects
    profileImage = new QLabel(this);
        profileImage->setObjectName("profileImageTwitItem");
        profileImage->setFixedSize(DEFAULT_PROFILE_IMAGE_SIZE, DEFAULT_PROFILE_IMAGE_SIZE);
    status = new QTextBrowser(this);
    status->setObjectName("statusTwitItem");
    status->setReadOnly(true);
    status->setOpenExternalLinks(true);
    status->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    status->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    animIcon = new QMovie(this);
        animIcon->setFileName(DEFAULT_THROBBER_ICON);
        animIcon->start();

    QSize iconBtnSize(DEFAULT_LABEL_BTN_SIZE, DEFAULT_LABEL_BTN_SIZE);
    favoritBtn = new QPushButton(this);
        favoritBtn->setFixedSize(iconBtnSize);
        favoritBtn->setObjectName("ListItemButton");
        favoritBtn->setCursor(QCursor(Qt::PointingHandCursor));
    favoritIcon = new QLabel(this);
        favoritIcon->setFixedSize(iconBtnSize);
        favoritIcon->setMovie(animIcon);
        favoritIcon->hide();
    replyBtn = new QPushButton(this);
        replyBtn->setFixedSize(iconBtnSize);
        replyBtn->setObjectName("ListItemButton");
        replyBtn->setCursor(QCursor(Qt::PointingHandCursor));
        replyBtn->hide();
    trashBtn = new QPushButton(this);
        trashBtn->setFixedSize(iconBtnSize);
        trashBtn->setObjectName("ListItemButton");
        trashBtn->setCursor(QCursor(Qt::PointingHandCursor));
        trashBtn->hide();
    trashIcon = new QLabel(this);
        trashIcon->setFixedSize(iconBtnSize);
        trashIcon->setMovie(animIcon);
        trashIcon->hide();


    createdAt = new QLabel(this);
        createdAt->setTextFormat(Qt::RichText);
        createdAt->setObjectName("dateTwitItem");
        createdAt->setOpenExternalLinks(true);
    fromL = new QLabel("from", this);
        fromL->setTextFormat(Qt::RichText);
        fromL->setObjectName("fromTwitItem");
    source = new QLabel(this);
        source->setTextFormat(Qt::RichText);
        source->setObjectName("sourceTwitItem");
        source->setOpenExternalLinks(true);

    //Layouts
    QVBoxLayout *profileImageLayout = new QVBoxLayout();
    profileImageLayout->addWidget(profileImage);
    profileImageLayout->addStretch(1);

    QVBoxLayout *iconBtnLayout = new QVBoxLayout();
    iconBtnLayout->addWidget(favoritBtn);
    iconBtnLayout->addWidget(favoritIcon);
    iconBtnLayout->addWidget(replyBtn);
    iconBtnLayout->addWidget(trashBtn);
    iconBtnLayout->addWidget(trashIcon);
    iconBtnLayout->addStretch(1);

    QHBoxLayout *postInfoLayout = new QHBoxLayout();
    postInfoLayout->addWidget(createdAt);
    postInfoLayout->addWidget(fromL);
    postInfoLayout->addWidget(source);
    postInfoLayout->addStretch(1);

    QHBoxLayout *status_profileImage_Layout = new QHBoxLayout();
    status_profileImage_Layout->addWidget(status);
    status_profileImage_Layout->addLayout(iconBtnLayout);

    QVBoxLayout *rightLayout = new QVBoxLayout();
    rightLayout->addLayout(status_profileImage_Layout);
    rightLayout->addLayout(postInfoLayout);
    rightLayout->addStretch(1);

    QHBoxLayout *mainLayout = new QHBoxLayout();
    mainLayout->addLayout(profileImageLayout);
    mainLayout->addLayout(rightLayout);

    //Remove all spacing between widgets
    profileImageLayout->setSpacing(0);
    iconBtnLayout->setSpacing(0);
    postInfoLayout->setSpacing(0);
    status_profileImage_Layout->setSpacing(0);
    rightLayout->setSpacing(0);
    mainLayout->setMargin(0);
    mainLayout->setSpacing(0);

    setLayout(mainLayout);

    connect(favoritBtn, SIGNAL(clicked()), this, SLOT(favoritClick()));
    connect(replyBtn, SIGNAL(clicked()), this, SLOT(replyClick()));
    connect(trashBtn, SIGNAL(clicked()), this, SLOT(trashClick()));

    indexPosition = 0;

    changeProfileImage();
}

void TwitListItem::changeProfileImage(const QString filename)
{
    profileImage->setPixmap(QPixmap(filename));
}

void TwitListItem::changeProfileImage(const QPixmap image)
{
    profileImage->setPixmap(image);
}

void TwitListItem::changeStatus(const QString text)
{
    status->setHtml(text);
//    status->setAlignment(Qt::AlignJustify);
    resize(width(), height());
}

void TwitListItem::changeCreatedAt(const QString date)
{
    createdAt->setText(date);
}

void TwitListItem::changeSource(const QString client)
{
    source->setText(client);
}

void TwitListItem::enableFavorit(const bool favorited)
{
    favoritIcon->hide();
    favoritBtn->show();
    QString icon;
    favorited ? icon = DEFAULT_FAVORIT_FULL_ICON : icon = DEFAULT_FAVORIT_EMPTY_ICON;
    favoritBtn->setIcon(QIcon(icon));
    favoritBtn->setProperty("favorited", favorited);
}

void TwitListItem::enableReply()
{
    replyBtn->setIcon(QIcon(DEFAULT_REPLY_ICON));
    replyBtn->show();
}

void TwitListItem::enableTrash()
{
    trashBtn->setIcon(QIcon(DEFAULT_TRASH_ICON));
    trashBtn->show();
}

void TwitListItem::replyClick()
{
    emit replyClicked(indexPosition);
}

void TwitListItem::favoritClick()
{
    qDebug() << "TwitListItem::favoritClick(const bool isFull)";
    favoritBtn->hide();
    favoritIcon->show();
    emit favoritClicked(indexPosition, favoritBtn->property("favorited").toBool());
}

void TwitListItem::trashClick()
{
    trashBtn->hide();
    trashIcon->show();
    emit trashClicked(indexPosition);
}

void TwitListItem::SetIndPosition(int index)
{
    indexPosition = index;
}

int TwitListItem::GetIndPosition()
{
    return indexPosition;
}

void TwitListItem::updateStatusSize()
{
    status->setFixedHeight(DEFAULT_STATUS_HEIGHT);
    if(status->verticalScrollBar()->maximum() > 0)
    {
        status->setFixedHeight(status->height() + status->verticalScrollBar()->maximum());
    }
}

void TwitListItem::resizeEvent(QResizeEvent *)
{
    updateStatusSize();
}

TwitListItem::~TwitListItem()
{
}
